package crm.copy;

import crm.modefy2.Data;

/**
 * 本次设计用链表存储CRM的数据，再笨类中记录相关处理的函数
 * @author ZHUAN4
 *
 */
public  class DataHand<T extends CustomerT> {
	 @SuppressWarnings("hiding")
	public class Node<T extends CustomerT> {
		private Node<T> prev;
		private Node<T> next;
		private T cusInfo;
		public Node<T> getPrev() {
			return prev;
		}
		public void setPrev(Node<T> prev) {
			this.prev = prev;
		}
		public Node<T> getNext() {
			return next;
		}
		public void setNext(Node<T> next) {
			this.next = next;
		}
		public T getCusInfo() {
			return cusInfo;
		}
		public void setCusInfo(T cusInfo) {
			this.cusInfo = cusInfo;
		}
		/**
		 * @param prev
		 * @param next
		 * @param cusInfo
		 */
		public Node(Node<T> prev, Node<T> next, T cusInfo) {
			super();
			this.prev = prev;
			this.next = next;
			this.cusInfo = cusInfo;
		}
		/**
		 * 
		 */
		public Node() {
			super();
		}
		/**
		 * @param cusInfo
		 */
		public Node(T cusInfo) {
			super();
			this.cusInfo = cusInfo;
		}
		public void setNextCustomer(T temp) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private Node<T> head;
	private Node<T> pointer;
	private int length;

	public Node<T> getPointer() {
		return pointer;
	}

	public void setPointer(Node<T> pointer) {
		this.pointer = pointer;
	}

	public Node<T> getHead() {
		return head;
	}

	public void setHead(Node<T> head) {
		this.head = head;
	}

	public DataHand() {
		length=10;
		head = new Node<T>(null);
		pointer = head;
	}

	/**
	 * 链表初始化函数
	 * @param initialT
	 * @param d 
	 */
	public  void  initCRM(T[] initialT){
		 
		@SuppressWarnings("unchecked")
		T[] temp=(T[]) InitialData.initialData();
		head.setCusInfo(temp[0]);
		pointer=head;
	   	for(int i=1;i<temp.length;i++){
			Node<T> tempNode=new Node<T>(temp[i]);
			pointer.setNext(tempNode);
			tempNode.setPrev(pointer);
			pointer=pointer.getNext(); 		
	   	}
	}
	public int getLengthCRM() {
		return length;
	}
	
	/**
	* get the num of node line at site of index
	* @param head
	* @param index
	* @return
	* @throws Exception
	*/
	public Node<T> getNodeOfIndex(int index)
	{
		int length=this.getLengthCRM();
		Node<T> temp = head;
		try {
			if(index>length||index<0) 
			{
				   throw new Exception("out of index!");
			}
			for(int i=1;i<=index;i++)
			{
				temp=temp.getNext();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
			
	}
	
	/**
	 * 查看所有的客户的信息，对应 无参L命令
	 * @throws Exception
	 */
   public  void lookCRM() {
	   int length=this.getLengthCRM();
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
        this.printCustomer(flags);
   }
   
   /**
	 * 通过顾客的id来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByIdCRM(int id) {
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{
			if(index<0&&index>=this.getLengthCRM())
			{
				System.out.println("The index found out of limit!");
			}
			else
			{
				int[] flags=new int[1];
				flags[0]=index;
				this.printCustomer(flags);
			}
		}
	}

	/**
	 * 通过顾客的name来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByNameCRM(String name) {
		int flag=0;
		int[] indexs = new int[this.getLengthCRM()];
		int count=0;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getCusInfo().getName().equals(name))
			{
				flag=1;
				indexs[count]=i;
				count++;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{   
			int[] flags=new int[count];
			for(int i=0;i<count;i++)
			{
				flags[i]=indexs[i];
			}
			this.printCustomer(flags);
		}
	}
	
	/**
	 * 将target 中的值赋值给 head
	 * @param head
	 * @param target
	 */
	public void exchangeCRM(T source, T target){
		source.setAddress(target.getAddress());
		source.setBirthday(target.getBirthday());
		source.setCall(target.getCall());
		source.setName(target.getName());
		source.setSex(target.getSex());
		source.setId(target.getId());
	}
	
	/**
	 * 向信息中添加一条数据  对应 A 操作
	 * @param add
	 */
	public void  addCRM(T add){
		if(this.getLengthCRM()==0)
		{
			T cusTemp=add;
			Node<T> nodeTemp=new Node<T>((T)cusTemp);
			head=nodeTemp;
			pointer=head;
			T.count++;
			length++;
		}
		else if(this.getLengthCRM()>=1)
		{
			T cusTemp=add;
			Node<T> nodeTemp=new Node<T>((T)cusTemp);
			pointer.setNext(nodeTemp);
			nodeTemp.setPrev(pointer);
			pointer=pointer.getNext();
			CustomerT.count++;
			length++;
		}
	}
	
	/**
	 * 通过id来在链表中查找删除相应的信息
	 * @param id
	 * @throws Exception
	 */
	public void deleteCRM(int id) {
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==1)
		{
			if(index>=0&&index<this.getLengthCRM())
			{
				if(index==0)
				{
					head=head.getNext();
					head.setPrev(null);
					length--;
				}
				else if(index==this.getLengthCRM()-1)
				{
					pointer=pointer.getPrev();
					pointer.setNext(null);
					length--;
				}
				else
				{
					Node<T> temp=this.getNodeOfIndex(index);
					temp.getPrev().setNext(temp.getNext());
					temp.getNext().setPrev(temp.getPrev());
					temp.setNext(null);
					temp.setPrev(null);
					length--;
				}
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}
		else
		{
			System.out.printf("The customer(id of %s) is not found!",id);
			System.out.println();
		}
					
	}
	
	/**
	 * 更新数据操作根据Id来查找信息并更新为update参数中的内容
	 * @param id
	 * @param update
	 * @throws Exception
	 */
	public void updateCRM(int id,T update) {
		
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM();i++)
		{
			if(this.getNodeOfIndex(i).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else{
			if(index>=0&&index<this.getLengthCRM())
			{
				int tempId=this.getNodeOfIndex(index).getCusInfo().getId();
				this.exchangeCRM(this.getNodeOfIndex(index).getCusInfo(), update);
				this.getNodeOfIndex(index).getCusInfo().setId(tempId);
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}	
	}
	
	/**
	 * 根据Id的大小排序,只做试视图
	 * @throws Exception
	 */
	public void sortByIdCRM() {
		int length=this.getLengthCRM();
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(flags[j]).getCusInfo().getId()<this.getNodeOfIndex(flags[j+1]).getCusInfo().getId())
//				if(this.getNodeOfIndex(j).getCusInfo().getId()>this.getNodeOfIndex(j+1).getCusInfo().getId())
				{
					int temp=flags[j];
					flags[j]=flags[j+1];
					flags[j+1]=temp;
				}
			}
		}
		System.out.println("·······················Sort by ID·····························");
		this.printCustomer(flags);
	}
	
	/**
	 * 根据name的大小排序
	 * @throws Exception
	 */
	public void sortByNameCRM() {

		int length=this.getLengthCRM();
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(flags[j]).getCusInfo().getName().compareTo(this.getNodeOfIndex(flags[j+1]).getCusInfo().getName())>0)
				{
					int temp=flags[j];
					flags[j]=flags[j+1];
					flags[j+1]=temp;
				}
			}
		}
		System.out.println("·······················Sort by Name···························");
		this.printCustomer(flags);
	}
	
	/**
	 * 打印控制函数，按照flags数组中的顺序打印出CustomerT的信息
	 * @param flags 记录顺序的数组
	 * @throws Exception 跑出数组月结异常
	 */
	public void printCustomer(int[] flags){
		
		System.out.println(" id        name    sex    birthday     call           address ");
	   	for(int i=0;i<flags.length;i++){
					System.out.printf("%3s",this.getNodeOfIndex(flags[i]).getCusInfo().getId());
					System.out.printf("%12s",this.getNodeOfIndex(flags[i]).getCusInfo().getName());
					System.out.printf("%6s   ",this.getNodeOfIndex(flags[i]).getCusInfo().getSex());
					System.out.printf("%10s",this.getNodeOfIndex(flags[i]).getCusInfo().getBirthday());
					System.out.printf("%15s     ",this.getNodeOfIndex(flags[i]).getCusInfo().getCall());
					System.out.printf("%-100s",this.getNodeOfIndex(flags[i]).getCusInfo().getAddress());
					System.out.println();
	   	}
	}



}
